<template>
  <!-- 感想的区域 -->
  <div id="square_card">
    <el-col
      :span="24"
      v-for="(item, index) in data"
      :key="index"
      style="margin-top: 20px"
    >
      <el-card>
        <div class="topInfo">
          <span>{{ item.nickname }}</span>
          <span>{{ item.title }}</span>
          <span>{{ formatTime(item.publishDate) }}</span>
        </div>
        <div class="likeCol">
          <img
            @click="like(item.id, username, !islike[index], index, $event)"
            :src="
              !islike[index]
                ? 'http://localhost:8081/likecollect/like.png'
                : 'http://localhost:8081/likecollect/liked.png'
            "
            alt="图片加载失败"
          />
          <span>({{ item.like_num }})</span>
          <img
            @click="
              collect(item.id, username, !iscollect[index], index, $event)
            "
            :src="
              !iscollect[index]
                ? 'http://localhost:8081/likecollect/collect.png'
                : 'http://localhost:8081/likecollect/collected.png'
            "
            alt="图片加载失败"
          />
          <span>({{ item.collect_num }})</span>
        </div>
        <el-divider></el-divider>
        <div class="pcontent">{{ item.content }} <br />{{ item.coverImg }}</div>
      </el-card>
    </el-col>
  </div>
</template>

<script>
// 格式化时间
import { format_time } from "@/common/format";
import axios from "axios";

export default {
  name: "square_card",
  props: ["articleData"],
  data() {
    return {
      // 点赞与收藏
      islike: [],
      iscollect: [],
      data: [],
      username: this.$store.state.username,
    };
  },
  watch: {
    articleData: {
      handler(n) {
        this.data = n;
        this.islike.splice(0, this.islike.length);
        this.iscollect.splice(0, this.iscollect.length);
        this.data.forEach((e) => {
          this.islike.push(e.like_ids.split(",").includes(this.username));
          this.iscollect.push(e.collect_ids.split(",").includes(this.username));
        });
      },
    },
  },
  methods: {
    // 格式化时间
    formatTime(publishDate) {
      return format_time("YYYY-mm-dd HH:MM", publishDate);
    },
    // 点赞
    async like(id, username, islikeBoolean, index, e) {
      e.preventDefault();
      this.islike[index] = !this.islike[index];
      this.data[index].like_num = this.islike[index]
        ? ++this.data[index].like_num
        : --this.data[index].like_num;
      await axios({
        method: "post",
        headers: {
          Authorization: this.$store.state.token,
        },
        url: "http://localhost:8081/my/article/like",
        params: {
          id,
          username,
          islike: islikeBoolean,
        },
      });
    },
    // 收藏
    async collect(id, username, iscollectBoolean, index, e) {
      e.preventDefault();
      this.iscollect[index] = !this.iscollect[index];
      this.data[index].collect_num = this.iscollect[index]
        ? ++this.data[index].collect_num
        : --this.data[index].collect_num;
      await axios({
        method: "post",
        headers: {
          Authorization: this.$store.state.token,
        },
        url: "http://localhost:8081/my/article/collect",
        params: {
          id,
          username,
          iscollect: iscollectBoolean,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  background-color: rgb(255, 251, 233);
  .topInfo {
    display: flex;
    :nth-child(2) {
      // background-color: #fff;
      color: rgb(13, 11, 11);
      font: 1.6rem sans-serif;
      margin-left: 13rem;
    }
    :nth-child(3) {
      position: absolute;
      right: 0;
      // background-color: #fff;
      margin-right: 0rem;
    }
  }
  .likeCol {
    position: absolute;
    right: 0;
    img {
      width: 1.2rem;
    }
  }
  .pcontent {
    margin-top: 2rem;
  }
}
</style>
